/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vista;

import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Joseph Tello Wong
 */
public class vistaNuevoRole extends javax.swing.JFrame {

    private DefaultTableModel modelo = new DefaultTableModel();

    /**
     * Creates new form vistaNuevoRole
     */
    public vistaNuevoRole() {
        initComponents();
        this.jComboBox1.setSelectedItem("NOT IDENTIFIED");
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        this.setResizable(false);
    }

    public JButton getBotonCrearRole() {
        return botonCrearRole;
    }

    public void obtenerRequisitos(List<String> requisitos) {
        requisitos.add(this.textoRole.getText());
        requisitos.add(this.jComboBox1.getSelectedItem() + " " + this.textoContraseñaOEsquema.getText());
        String privilegios = "";
        boolean check = false;
        modelo = (DefaultTableModel) this.tablaPrivilegios.getModel();
        for (int i = 0; i < modelo.getRowCount(); i++) {
            check = (boolean) modelo.getValueAt(i, 1);
            if (check) {
                if ("".equals(privilegios)) {
                    privilegios = privilegios + modelo.getValueAt(i, 0);
                } else {
                    privilegios = privilegios + ", " + modelo.getValueAt(i, 0);
                }
            }
            modelo.setValueAt(false, i, 1);
        }
        this.tablaPrivilegios.setModel(modelo);
        this.tablaPrivilegios.repaint();
        requisitos.add(privilegios);
        
        this.textoRole.setText("");
        this.jComboBox1.setSelectedIndex(1);
        this.textoContraseñaOEsquema.setText("");
        
        System.out.println("Requisitos de nuevo rol: " + requisitos);
    }

    public void cargarPrivilegios(List<String> privilegios) {
        modelo = (DefaultTableModel) this.tablaPrivilegios.getModel();
        for (String priv : privilegios) {
            modelo.addRow(new Object[]{priv, false});
        }
        this.tablaPrivilegios.setModel(modelo);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        textoRole = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        tablaPrivilegios = new javax.swing.JTable();
        jLabel2 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        textoContraseñaOEsquema = new javax.swing.JTextField();
        botonCrearRole = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Crear nuevo rol");

        jLabel1.setText("Nombre del nuevo rol:");

        tablaPrivilegios.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Privilegio", "Otorgar"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Object.class, java.lang.Boolean.class
            };
            boolean[] canEdit = new boolean [] {
                false, true
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tablaPrivilegios.getTableHeader().setReorderingAllowed(false);
        jScrollPane1.setViewportView(tablaPrivilegios);
        tablaPrivilegios.getColumnModel().getColumn(0).setResizable(false);
        tablaPrivilegios.getColumnModel().getColumn(0).setPreferredWidth(230);
        tablaPrivilegios.getColumnModel().getColumn(1).setResizable(false);

        jLabel2.setText("Identified:");

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "IDENTIFIED BY", "NOT IDENTIFIED", "USING", "EXTERNALLY", "GLOBALLY" }));
        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });

        textoContraseñaOEsquema.setEnabled(false);

        botonCrearRole.setText("Crear rol");

        jLabel3.setText("Privilegios del nuevo rol:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(botonCrearRole))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                                .addGroup(layout.createSequentialGroup()
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabel1)
                                        .addComponent(jLabel2))
                                    .addGap(18, 18, 18)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addComponent(textoContraseñaOEsquema)
                                        .addComponent(textoRole)
                                        .addComponent(jComboBox1, 0, 183, Short.MAX_VALUE))))
                            .addComponent(jLabel3))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(37, 37, 37)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(textoRole, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(textoContraseñaOEsquema, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(19, 19, 19)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 220, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addComponent(botonCrearRole)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed
        this.textoContraseñaOEsquema.setEnabled(false);
        if ("IDENTIFIED BY".equals(this.jComboBox1.getSelectedItem()) || "USING".equals(this.jComboBox1.getSelectedItem())) {
            this.textoContraseñaOEsquema.setEnabled(true);
        }
    }//GEN-LAST:event_jComboBox1ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(vistaNuevoRole.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(vistaNuevoRole.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(vistaNuevoRole.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(vistaNuevoRole.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new vistaNuevoRole().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botonCrearRole;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tablaPrivilegios;
    private javax.swing.JTextField textoContraseñaOEsquema;
    private javax.swing.JTextField textoRole;
    // End of variables declaration//GEN-END:variables
}
